package org.eclipseuseradmin.ui.wizards;

import org.eclipse.core.databinding.Binding;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.PojoObservables;
import org.eclipse.core.databinding.validation.IValidator;
import org.eclipse.core.databinding.validation.ValidationStatus;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.jface.databinding.swt.SWTObservables;
import org.eclipse.jface.databinding.wizard.WizardPageSupport;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipseuseradmin.internal.ui.UserAdminUiPlugin;
import org.eclipseuseradmin.internal.ui.wizards.WizardsMessages;
import org.eclipseuseradmin.ui.model.RoleElementModel;

public class NewRoleElementWizardPage extends WizardPage {

	private DataBindingContext bindingContext;

	protected Composite mainComposite;

	protected Label nameLabel;

	protected Text nameText;

	public NewRoleElementWizardPage() {

		this("main", WizardsMessages.get().NewRoleElementWizardPage_title,
				WizardsMessages.get().NewRoleElementWizardPage_description,
				UserAdminUiPlugin.getImageDescriptor("/icons/user.gif"));
	}

	public NewRoleElementWizardPage(String pageName, String title,
			String description, ImageDescriptor titleImage) {

		super(pageName, title, titleImage);
		setDescription(description);
		bindingContext = new DataBindingContext();
		WizardPageSupport.create(this, bindingContext);
	}

	public void createControl(Composite parent) {

		mainComposite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		mainComposite.setLayout(layout);

		nameLabel = new Label(mainComposite, SWT.NONE);
		nameLabel.setText(WizardsMessages.get().NewRoleElementWizardPage_name);

		nameText = new Text(mainComposite, SWT.BORDER);
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		nameText.setLayoutData(layoutData);
		bindNameValue();

		setControl(mainComposite);
	}

	protected void bindNameValue() {

		final RoleElementModel model = getNewRoleElementWizard().getModel();

		UpdateValueStrategy updateValueStrategy = new UpdateValueStrategy();
		updateValueStrategy.setBeforeSetValidator(new IValidator() {
			public IStatus validate(Object value) {
				if (((String) value).length() == 0) {
					return ValidationStatus
							.error(WizardsMessages.get().NewRoleElementWizardPage_error_name_empty);
				}
				if (model.exists(((String) value))) {
					return ValidationStatus
							.error(WizardsMessages.get().NewRoleElementWizardPage_error_name_exists);
				}
				return ValidationStatus.OK_STATUS;
			}
		});
		Binding binding = bindingContext.bindValue(SWTObservables.observeText(
				nameText, SWT.FocusOut), PojoObservables.observeValue(model,
				"name"), updateValueStrategy, null);
	}

	protected BaseNewRoleElementWizard getNewRoleElementWizard() {
		return ((BaseNewRoleElementWizard) getWizard());
	}

	protected DataBindingContext getBindingContext() {
		return bindingContext;
	}

}
